package com.book.activeMQ.consumers;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.book.dto.TempOrderBookDto;
import com.book.service.domain.OrderCreateTempMqService;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/6/1 10:22 上午
 * @Project_Name graduation-project
 */
@Component
@Slf4j
public class SaveTempOrderMq implements MessageListener {
    @Autowired
    private OrderCreateTempMqService createTempMqService;
    @SneakyThrows
    @Override
    public void onMessage(Message message) {
        log.info("SaveOrderDetail#message:{}",message);
        String text = ((TextMessage) message).getText();
        Map<String, JSONArray> object = JSONObject.parseObject(text,Map.class);
        log.info("SaveTempOrderMq object:{}",object);
        for (String key: object.keySet()
             ) {
            List<TempOrderBookDto> tempOrderList = Lists.newArrayList();
            log.info("SaveTempOrderMq map key:{}",key);
            JSONArray jsonObject = object.get(key);
            List list = JSONObject.toJavaObject(jsonObject,List.class);
            log.info("SaveTempOrderMq map value:{}",list);
            list.forEach(m->{
                TempOrderBookDto tempOrderBookDto = JSONObject.parseObject(m.toString(), TempOrderBookDto.class);
                tempOrderList.add(tempOrderBookDto);
            });
            //保存
            log.info("SaveTempOrderMq tempOrderList:{}",tempOrderList);
            createTempMqService.save(tempOrderList,key);
        }

    }
}
